if(!de) var de={};
if(!de.soerenrinne) de.soerenrinne={};
if(!de.soerenrinne.GoogleButtons) de.soerenrinne.GoogleButtons={};
if(!de.soerenrinne.GoogleButtons.Settings) de.soerenrinne.GoogleButtons.Settings={};

de.soerenrinne.GoogleButtons.Settings = function (){
	
	var pub = {};

	googlebuttonsPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("de.soerenrinne.googlebuttons.");
	
	var servicesList = googlebuttonsPrefs.getCharPref('wholeshebang');
	var soerenrinneServicesArray = new Array();
	soerenrinneServicesArray = servicesList.split(',');
	soerenrinneServicesArray.sort(function(x,y){ 
		var a = String(x).toUpperCase(); 
		var b = String(y).toUpperCase(); 
		if (a > b) 
			return 1 
		if (a < b) 
			return -1 
		return 0; 
	});
	var soerenrinneNumberOfServices = soerenrinneServicesArray.length;
								
	pub.loadOptions = function() {
	
		try {
			numberInColumn = soerenrinneNumberOfServices / 4;
			// Add items
			var vbox1 = document.getElementById("vbox1");
			var vbox2 = document.getElementById("vbox2");
			var vbox3 = document.getElementById("vbox3");
			var vbox4 = document.getElementById("vbox4");
			
			for (var j=0; j<soerenrinneNumberOfServices; j++) {
				var id = soerenrinneServicesArray[j];
				var idStripped = id.replace(/\ /g, "").toLowerCase();
				var tooltip = googlebuttonsPrefs.getCharPref(id + "_tooltip");
				var child = create_Checkbox(idStripped, " "+id, tooltip);
				if (document.getElementById(idStripped) == null) {
					if (j < numberInColumn)
						vbox1.appendChild(child);
					else if (j < 2*numberInColumn)
						vbox2.appendChild(child);
					else if (j < 3*numberInColumn)
						vbox3.appendChild(child);
					else
						vbox4.appendChild(child);
				}
				document.getElementById(idStripped).checked = googlebuttonsPrefs.getBoolPref(id);
			}			
			
			document.getElementById("settings").checked = googlebuttonsPrefs.getBoolPref("settings");
			document.getElementById("focustab").checked = googlebuttonsPrefs.getBoolPref("focustab");
			document.getElementById("openintab").checked = googlebuttonsPrefs.getBoolPref("openintab");
			document.getElementById("alwayshttps").checked = googlebuttonsPrefs.getBoolPref("alwayshttps");
			document.getElementById("appsDomainName").value = googlebuttonsPrefs.getCharPref("appsDomainName");
			document.getElementById("appsMail").checked = googlebuttonsPrefs.getBoolPref("appsMail");
			document.getElementById("appsCal").checked = googlebuttonsPrefs.getBoolPref("appsCal");
			document.getElementById("appsDocs").checked = googlebuttonsPrefs.getBoolPref("appsDocs");
			document.getElementById("appsContacts").checked = googlebuttonsPrefs.getBoolPref("appsContacts");
			document.getElementById("appsSites").checked = googlebuttonsPrefs.getBoolPref("appsSites");
			document.getElementById("appsTasks").checked = googlebuttonsPrefs.getBoolPref("appsTasks");
			
			if(googlebuttonsPrefs.getBoolPref('orient_horizontally') == true) {
				document.getElementById('settingsOrientation').selectedItem = document.getElementById('orient_horizontally');
			} else {
				document.getElementById('settingsOrientation').selectedItem = document.getElementById('orient_vertically');
			}
			
			if(googlebuttonsPrefs.getBoolPref('dropdown') == false) {
				document.getElementById('settingsDisplay').selectedItem = document.getElementById('toolbar');
			} else {
				document.getElementById('settingsDisplay').selectedItem = document.getElementById('dropdown');
			}
			
			document.getElementById("noarrow").checked = googlebuttonsPrefs.getBoolPref("noarrow");
			
			if(googlebuttonsPrefs.getBoolPref('subtitleposbottom') == false) {
				document.getElementById('settingsSubtitlesPosition').selectedItem = document.getElementById('subtitlesRight');
			} else {
				document.getElementById('settingsSubtitlesPosition').selectedItem = document.getElementById('subtitlesBottom');
			}
			
		} catch (e) {
			alert("Error on loading settings:\n" + e.name + ", " + e.message);
		}
	}
	
	pub.saveOptions = function() {
	
		try {			
			for (var i=0;i<soerenrinneNumberOfServices;i++)
			{
				var id = soerenrinneServicesArray[i];
				var idStripped = id.replace(/\ /g, "").toLowerCase();
				if (document.getElementById(idStripped).checked)
					googlebuttonsPrefs.setBoolPref(id, true);
				else
					googlebuttonsPrefs.setBoolPref(id, false);
			}
			
			(document.getElementById("settings").checked) ? googlebuttonsPrefs.setBoolPref("settings", true) : googlebuttonsPrefs.setBoolPref("settings", false);
					
			(document.getElementById("focustab").checked) ? googlebuttonsPrefs.setBoolPref("focustab", true) : googlebuttonsPrefs.setBoolPref("focustab", false);					
			(document.getElementById("openintab").checked) ? googlebuttonsPrefs.setBoolPref("openintab", true) : googlebuttonsPrefs.setBoolPref("openintab", false);
			
			(document.getElementById('orient_horizontally').selected == true) ? googlebuttonsPrefs.setBoolPref('orient_horizontally', true) : googlebuttonsPrefs.setBoolPref('orient_horizontally', false);
			
			(document.getElementById('subtitlesBottom').selected == true) ? googlebuttonsPrefs.setBoolPref('subtitleposbottom', true) : googlebuttonsPrefs.setBoolPref('subtitleposbottom', false);
			
			(document.getElementById('noarrow').checked) ? googlebuttonsPrefs.setBoolPref('noarrow', true) : googlebuttonsPrefs.setBoolPref('noarrow', false);
			
			(document.getElementById('toolbar').selected == true) ? googlebuttonsPrefs.setBoolPref('dropdown', false) : googlebuttonsPrefs.setBoolPref('dropdown', true);
			
			(document.getElementById('alwayshttps').checked) ? googlebuttonsPrefs.setBoolPref('alwayshttps', true) : googlebuttonsPrefs.setBoolPref('alwayshttps', false);
			
			googlebuttonsPrefs.setCharPref("appsDomainName", document.getElementById('appsDomainName').value);
			
			(document.getElementById('appsMail').checked) ? googlebuttonsPrefs.setBoolPref('appsMail', true) : googlebuttonsPrefs.setBoolPref('appsMail', false);
			(document.getElementById('appsCal').checked) ? googlebuttonsPrefs.setBoolPref('appsCal', true) : googlebuttonsPrefs.setBoolPref('appsCal', false);
			(document.getElementById('appsDocs').checked) ? googlebuttonsPrefs.setBoolPref('appsDocs', true) : googlebuttonsPrefs.setBoolPref('appsDocs', false);
			(document.getElementById('appsContacts').checked) ? googlebuttonsPrefs.setBoolPref('appsContacts', true) : googlebuttonsPrefs.setBoolPref('appsContacts', false);
			(document.getElementById('appsSites').checked) ? googlebuttonsPrefs.setBoolPref('appsSites', true) : googlebuttonsPrefs.setBoolPref('appsSites', false);
			(document.getElementById('appsTasks').checked) ? googlebuttonsPrefs.setBoolPref('appsTasks', true) : googlebuttonsPrefs.setBoolPref('appsTasks', false);
			
			var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
			prefService.savePrefFile(null);

			
		} catch (e) {
			alert("Error on saving settings:\n" + e.name + ", " + e.message);
		}
		
		window.close();
	}
	
	create_Checkbox = function(aCheckboxID, aCheckboxLabel, aCheckboxTooltip) {
		const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
	  	var item = document.createElementNS(XUL_NS, "checkbox"); // create a new XUL checkbox
	  	item.setAttribute("id", aCheckboxID);
		item.setAttribute("label", aCheckboxLabel);
		item.setAttribute("tooltiptext", aCheckboxTooltip);
		item.setAttribute("src", "chrome://googlebuttons/skin/" + aCheckboxID + ".gif");
	  	return item;
	}
	
	return pub;
}();
